import bge
import bgl

scene = bge.logic.getCurrentScene()
controller = bge.logic.getCurrentController()

LMB         = controller.sensors["LMB"]
mouseOver   = controller.sensors["MouseOver"]
exitGUI     = controller.actuators["Msg"]
Txt         = scene.objects["GUIstatetxt"]

Label       = scene.objects["GUIstateLabel"]

owner       = controller.owner


states = ""


if 'states' in bge.selectObj:
    target = (bge.selectObj['stateOwner'])

    for State in bge.selectObj['states']:
        states += State +"\n"
else:
    states = "No states found for this object"    

Txt.text = str(states)
Label.text = "Available states for %s:" %(bge.selectObj.name)

if LMB.positive and mouseOver.hitObject != None:
    if ( mouseOver.hitObject.name[0:11]) == "StateButton":
        index = int(mouseOver.hitObject.name.split(".")[1])
        target['State'] = bge.selectObj['states'][index]
        controller.activate(exitGUI)
